package lingou.homework.postbar.controller.proto;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lingou.homework.postbar.controller.tool.JsonHelper;
import lingou.homework.postbar.domain.Post;
import lingou.homework.postbar.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class PostView {
    @Autowired
    PostService postService;
    public Post ofAnnoPost(Post post, long currentUserId) {
        if(post.getUserId() != currentUserId && post.getAnon() > 0){
            post.setUserId(-1l);
        }
        return post;
    }

    public JsonNode ofPubBoxPost(Post post, long currentUserId) {
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode jsonNode = objectMapper.valueToTree(post);
        jsonNode.set("pub", BooleanNode.valueOf(postService.isBoxPostPub(currentUserId, post.getPostId())));
        return jsonNode;
    }
//    public JsonNode ofBoxPost(Post post) {
//        return JsonHelper.attach(post, JsonHelper.of("ownerId", postService.));
////        return Map.ofEntries(
////                Map.entry("postId", post.getPostId()),
////                Map.entry("time", post.getTime()),
////                Map.entry("type", post.getType()),
////                Map.entry("likes", post.getLikes()),
////                Map.entry("dislikes", post.getDislikes()),
////                Map.entry("status", post.getStatus()),
////                Map.entry("snapshotId", post.getSnapshotId()),
////                Map.entry("title", post.getPreview())
////        );
//    }
}
